package com.example.demo.dao;

import com.example.demo.pojo.Department;
import com.example.demo.pojo.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

//员工dao
@Repository
public class EmployeeDao {
    @Autowired
    private DepartmentDao departmentDao;

    private static Map<Integer, Employee> employees = null;

    static {
        employees = new HashMap<Integer, Employee>();//员工信息表

        employees.put(001, new Employee(001, "张三", "123@123.com", 0, new Department(10001, "教学部")));
        employees.put(002, new Employee(002, "李四", "223@123.com", 1, new Department(10001, "教学部")));
        employees.put(003, new Employee(003, "王五", "323@123.com", 0, new Department(10001, "教学部")));
        employees.put(004, new Employee(004, "麻子", "423@123.com", 1, new Department(10001, "教学部")));
        employees.put(005, new Employee(005, "胡子", "523@123.com", 0, new Department(10001, "教学部")));
        employees.put(006, new Employee(006, "孙子", "623@123.com", 1, new Department(10001, "教学部")));
    }

    //主键自增
    private static Integer initId = 007;

    //增加一个员工
    public void save(Employee employee) {
        if (employee.getId() == null) {
            employee.setId(initId++);
        }
        employee.setDepartment(departmentDao.getDepartmentById(employee.getDepartment().getId()));
        employees.put(employee.getId(), employee);
    }

    //获取全部员工
    public Collection<Employee> getAll() {
        return employees.values();
    }

    //通过id查询员工
    public Employee getEmployeeById(Integer id) {
        return employees.get(id);
    }

    //修改
    public void updateEmp(Employee employee) {
        employees.replace(employee.getId(), employee);
    }


    //删除员工
    public void delete(Integer id) {
        employees.remove(id);
    }
}